package flc.ast.fragment;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.teleprompter.lib.TpFloatService;
import com.stark.teleprompter.lib.TpSettingManager;
import com.stark.teleprompter.lib.db.TaiciBean;
import com.stark.teleprompter.lib.db.TaiciDbHelper;

import java.util.List;

import flc.ast.R;
import flc.ast.activity.MyLinesActivity;
import flc.ast.databinding.FragmentTeleprompterBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.utils.FastClickUtil;
import stark.common.basic.utils.StkPermissionHelper;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TeleprompterFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TeleprompterFragment extends BaseNoModelFragment<FragmentTeleprompterBinding> {

    private List<TaiciBean> mTaiciBeanList;


    @Override
    protected int onCreate() {
        return R.layout.fragment_teleprompter;
    }

    @Override
    protected void initView() {
        mDataBinding.etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String str = s.toString();
                int length = str.length();
                mDataBinding.tvTeleprompterTip.setText(getString(R.string.teleprompter_tip, length, length / 60, length % 60));
            }
        });
        mDataBinding.tvClearLines.setOnClickListener(this);
        mDataBinding.ivRecord.setOnClickListener(this);
        mDataBinding.tvSaveLines.setOnClickListener(this);
        mDataBinding.tvInstantShot.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        if (FastClickUtil.isFastClick()) return;
        switch (v.getId()) {
            case R.id.tvSaveLines://保存台词
                saveLines();
                break;
            case R.id.tvClearLines://清空台词
                mDataBinding.etContent.setText("");
                mDataBinding.tvTeleprompterTip.setText(getString(R.string.teleprompter_tip, 0, 0, 0));
                break;
            case R.id.ivRecord://我的台词
                startActivity(MyLinesActivity.class);
                break;
            case R.id.tvInstantShot:
                getDialogPermission();
                break;
        }
    }

    @Override
    protected void initData() {
        TaiciDbHelper.getTaiciBeans().observe(this, new Observer<List<TaiciBean>>() {
            @Override
            public void onChanged(List<TaiciBean> taiciBeans) {
                mTaiciBeanList = taiciBeans;
            }
        });
        mDataBinding.tvTeleprompterTip.setText(getString(R.string.teleprompter_tip, 0, 0, 0));
    }

    //保存台词
    private void saveLines() {
        String content = mDataBinding.etContent.getText().toString();
        if (TextUtils.isEmpty(content)) {
            ToastUtils.showShort(R.string.please_input_content);
        } else {
            TaiciBean saveBean = new TaiciBean();
            saveBean.setCreateTime(System.currentTimeMillis());
            saveBean.setTitle(content);
            saveBean.setContent(content);
            //新增
            TaiciDbHelper.insert(saveBean);
            ToastUtils.showShort(getString(R.string.save_success));
        }
    }

    //获取提词器弹框权限
    private void getDialogPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            StkPermissionHelper.permission(Manifest.permission.SYSTEM_ALERT_WINDOW)
//                    .reqPermissionDesc(getString(R.string.get_window_permission))
//                    .callback(new StkPermissionHelper.ACallback() {
//                        @Override
//                        public void onGranted() {
//                            TpFloatService.start(mContext);
//                        }
//
//                        @Override
//                        public void onDenied(boolean needGoSettings) {
//                            ToastUtils.showShort(R.string.no_permission);
//                        }
//                    }).request();
            PermissionUtils.requestDrawOverlays(new PermissionUtils.SimpleCallback() {
                @Override
                public void onGranted() {
                    startFloatService();
                }

                @Override
                public void onDenied() {
                    ToastUtils.showShort(R.string.no_permission);
                }
            });
        } else {
            startFloatService();
        }
    }

    private void startFloatService() {
        if (mTaiciBeanList == null || mTaiciBeanList.size() == 0) {
            ToastUtils.showShort(getString(R.string.please_add_lines));
            return;
        }
        //这里一定要将选中的台词设置进去后，再启动悬浮窗服务
        TpSettingManager.getInstance().setSelTaiciBean(mTaiciBeanList.get(0));
        TpFloatService.start(mContext);
    }

}